﻿/*
Copyright 2011 Shane Ng

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using freemarker.template;
using freemarker.template.utility;
using java.math;

namespace Freemarker.Net {

    public static class MethodExtensions {
        public static int GetHashKey(this Type[] types) {
            int result = 0;
            for (int i = 0; i < types.Length; i++) {
                result ^= types[i].GetHashCode() + i;
            }
            return result;
        }

        public static List<object> GetArgs(this java.util.List l) {
            List<object> args = new List<object>();
            for (int i = 0, n = l.size(); i < n; i++) {
                if (l.get(i) is TemplateModel) {
                    var arg = (TemplateModel)l.get(i);
                    if (arg is TemplateNumberModel) {
                        var data = DeepUnwrap.unwrap(arg);
                        if (data is BigDecimal) {
                            if (arg.ToString().Contains('.')) {
                                args.Add(((BigDecimal)data).doubleValue());
                            }
                            else {
                                args.Add(((BigDecimal)data).intValue());
                            }
                        }
                        else {
                            args.Add(data);
                        }
                    }
                    else {
                        args.Add(DeepUnwrap.unwrap(arg));
                    }
                }
                else {
                    args.Add(l.get(i));
                }
            }
            return args;
        }
    }
}
